﻿var GetPrizeNameJson;
var pageSize = 15;
$(document).ready(function () {
    Search(1);
    $("#btnSele").click(function () {
        //        if ($("#txtIMSI").val() == "") {
        //            $.dialog.alert("请输入查询条件！")
        //        }
        //        else
        PageClickFunc(1);
    })

    $("#txtRemark").focus(function () {
        if ($(this).val() == "输入备注查询") {
            $(this).val("");
        }
    })

    $("#txtRemark").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("default"));
        }

    })

    $("#btnAdd").click(function () {
        if ($("#lblUserIMSI").attr("accesskey") != "" && $("#lblUserIMSI").attr("accesskey") != "0")
            Edit(null);
        else
            $.dialog.alert("请输入添加用户的信息！")
    })

    $("#seleByType").change(function () {
        PageClickFunc(1);
    })
    $.ajax({
        type: "post",
        data: { action: "GetPrizeName" },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                GetPrizeNameJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
    PageClickFunc = function (pageclickednumber) {
        Search(pageclickednumber);
    }
    function Search(pageclickednumber) {
        var remark = $("#txtRemark").val() == $("#txtRemark").attr("default") ? "" : $("#txtRemark").val();

        $.ajax({
            type: "post",
            data: { action: "Select", userSearchType: $("#selUser").val(), state: $("#seleByType").val(), value: $("#txtIMSI").val(), pageIndex: pageclickednumber, pageSize: pageSize, remark: remark },
            dataType: "json",
            url: "../../Ajax/PrizeTaskReocrdAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data != null) {
                    var json = data.json;
                    var tbody = "";
                    var pageCount = data.flag;
                    $("#lblUserIMSI").attr("accesskey", data.userID);
                    $.each(json, function (k, v) {
                        tbody += "<tr id='" + v.Id + "'>"
                        tbody += "<td><lable name='lblUserid' id='" + v.Userid + "'>";
                        tbody += v.UserIMSI;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblImei'>";
                        tbody += v.UserImei;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblMac'>";
                        tbody += v.UserMac;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblUserAccount'>";
                        tbody += v.UserAccount;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblPrizeid' id='" + v.Prizeid + "'>";
                        tbody += v.PrizeName;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblCreatetime'>";
                        tbody += v.Createtime;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblEditTime'>";
                        tbody += v.EditTime;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblStuas' id='" + v.Stuas + "'>";
                        tbody += v.PrizeTaskReocrdState;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblTaskID' id='" + v.TaskID + "'>";
                        tbody += v.TaskName;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblRemark'>";
                        tbody += v.Remark;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblPrizeCount'>";
                        tbody += v.PrizeCount;
                        tbody += "</lable></td>";
                        tbody += "<td>";
                        tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                        tbody += "</td>";
                        tbody += "</td>";
                        tbody += "</tr>"
                    })
                    $("#tb2").empty().append(tbody);
                    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });

                }
                else
                    $("#lblUserIMSI").attr("accesskey", "");
            },
            complete: function () {
            },
            error: function () {
            }
        })

    }
})

function ChangeChanceType(contr) {
    var parent = $(contr).parent().parent();
    var IMSI = $(parent).find("lable[name=lblIMSI]").first().text();
    var ID = $(parent).find("lable[name=lblIMSI]").first().attr("id");
    LoadChance(ID, IMSI);
}
function LoadChance(id, UserIMSI) {
    if (id > 0) {
        $("#lblUserIMSI").text(UserIMSI);
        $("#lblUserIMSI").attr("accesskey", id);
        $.ajax({
            type: "post",
            data: { action: "GetList", typeID: id }, //任务id
            dataType: "json",
            url: "../../Ajax/PrizeTaskReocrdAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data != null) {
                    var json = data.json;
                    var tbody = "";
                    $.each(json, function (k, v) {
                        tbody += "<tr id='" + v.Id + "'>"
                        tbody += "<td><lable name='lblUserid' id='" + v.UserID + "'>";
                        tbody += v.UserISMI;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblPrizeid' id='" + v.Prizeid + "'>";
                        tbody += v.PrizeName;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblCreatetime'>";
                        tbody += v.Createtime;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblEditTime'>";
                        tbody += v.EditTime;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblStuas' id='" + v.Stuas + "'>";
                        tbody += v.PrizeTaskReocrdState;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblTaskID' id='" + v.TaskID + "'>";
                        tbody += v.TaskName;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblRemark'>";
                        tbody += v.Remark;
                        tbody += "</lable></td>";
                        tbody += "<td><lable name='lblPrizeCount'>";
                        tbody += v.PrizeCount;
                        tbody += "</lable></td>";
                        tbody += "<td>";
                        tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                        tbody += "</td>";
                        tbody += "</td>";
                        tbody += "</tr>"
                    })
                    $("#tb2").empty().append(tbody);

                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblUserid]").first();
    Edit(lbl);
}
//选中第一个下拉框
function ChangeTaskName(value) {
    var value = $("#selState").find("option:selected").text();
    if (value == '比赛币')
        isMatch = true;
    else
        isMatch = false;
    $("#tdTaskName").empty().append(InitTask());
}
//选择第二个下拉框的值
function InitTask(value) {
    var sel = "";
    if (isMatch)
        sel += "<select id='selTaskName'>";
    else
        sel += "<select id='selTaskName' style='display:none'>";

    json = PrizeTaskChanged()
    $.each(json, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    sel += "</select>";
    return sel;
}
function PrizeTaskChanged() {
    var json = null;
    $.ajax({
        type: "post",
        data: { action: "GetListByPrize" },
        dataType: "json",
        url: "../../Ajax/TaskAjax.ashx",
        async: false,
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                json = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    return json;
}
var isMatch = false;
function InitSelectPrizeName(value) {
    var sel = "";
    $.each(GetPrizeNameJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value) {
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
            if (v.Name == "比赛币")
                isMatch = true;
            else
                isMatch = false;
        }
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortNum = $(lbl).parent().parent().find("lable[name=lblUserid]");
    var sortId = $(lbl).parent().parent().find("lable[name=lblUserid]").attr("id"); //自身ID
    var UserID = $(lbl).parent().parent().attr("id")//用户ID
    var prizeID = $(lbl).parent().parent().find("lable[name=lblPrizeid]").attr("id"); //自身ID
    var taskID = $(lbl).parent().parent().find("lable[name=lblTaskID]").attr("id")
    var content = "";
    var data = new Date();
    if (lbl != null) {
        content = "<table><tr><td>物品类型：<select id='selState' onchange='ChangeTaskName()' >" + InitSelectPrizeName(prizeID) + "</select></td><td id='tdTaskName'>" + InitTask(taskID) + "</td></tr>"
        content += "<tr><td colspan='2'>创建时间：<lable name='lblCreateTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + "</lable></td></tr>"
        content += "<tr><td colspan='2'>更新时间：<lable name='txtEndTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + "</lable></td></tr>"
        content += "<tr><td colspan='2'>描述：<input type='text' id='txtRemark' value='" + $(lbl).parent().parent().find("lable[name=lblRemark]").text() + "' style='width:200px' /></td></tr>"
        content += "<tr><td colspan='2'>数量：<input type='text' id='txtPrizeCount' value='" + $(lbl).parent().parent().find("lable[name=lblPrizeCount]").text() + "' style='width:200px' /></td></tr></table>"
    }
    else {
        content = "<table><tr><td>物品类型：<select id='selState' onchange='ChangeTaskName()' >" + InitSelectPrizeName() + "</select></td><td id='tdTaskName'>" + InitTask() + "</td></tr>"
        content += "<tr><td colspan='2'>创建时间：<lable name='lblCreateTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + "</lable></td></tr>"
        content += "<tr><td colspan='2'>更新时间：<lable name='txtEndTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + "</lable></td></tr>"
        content += "<tr><td colspan='2'>描述：<input type='text' id='txtRemark' style='width:200px' /></td></tr>"
        content += "<tr><td colspan='2'>数量：<input type='text' id='txtPrizeCount' style='width:200px' /></td></tr></table>"

    }

    $.dialog({
        title: "物品添加设置模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $("#txtRemark"), sortId, UserID);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum, sortId, UserID) {
    var objectId = UserID;
    var UserID = $("#lblUserIMSI").attr("accesskey"); //用户ID
    var prizeID = $(sortNum).parent().parent().parent().find("#selState[option:selected]").val();
    var CreateTime = $(sortNum).parent().parent().parent().find("lable[name=lblCreateTime]").first().text();
    var txtEndTime = $(sortNum).parent().parent().parent().find("lable[name=txtEndTime]").first().text();
    var txtRemark = $(sortNum).parent().parent().parent().find("#txtRemark").first().val();
    var txtPrizeCount = $(sortNum).parent().parent().parent().find("#txtPrizeCount").first().val();
    var UserIMSI = $("#lblUserIMSI").text(); //用户IMSI
    var PrizeName = $(sortNum).parent().parent().parent().find("#selState>option:selected").text();
    if (PrizeName == '比赛币') {
        var taskID = $(sortNum).parent().parent().parent().find("#selTaskName[option:selected]").val();
        var taskName = $(sortNum).parent().parent().parent().find("#selTaskName>option:selected").text();
    }
    var PrizeTaskReocrdStates = "未领取";
    var PrizeTaskReocrdState = $(contr).parent().parent().find("#lable[name=lblStuas]").text();
    var TaskName = $(sortNum).parent().parent().parent().find("#lable[name=lblTaskID]").text();
    var txtIsManuallyAdd = true; //是否手动输入
    var data = contr == null ? { action: "Add", UserID: UserID, prizeID: prizeID, txtRemark: txtRemark, CreateTime: CreateTime, txtEndTime: txtEndTime, txtPrizeCount: txtPrizeCount, txtIsManuallyAdd: txtIsManuallyAdd, taskID: taskID} :
              { action: "Update", id: objectId, prizeID: prizeID, txtRemark: txtRemark, txtPrizeCount: txtPrizeCount, taskID: taskID };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/PrizeTaskReocrdAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr id='" + data + "'>"
                tbody += "<td><lable name='lblUserid'>";
                tbody += UserIMSI;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblPrizeid' id='" + prizeID + "'>";
                tbody += PrizeName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblCreatetime'>";
                tbody += CreateTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblEditTime'>";
                tbody += txtEndTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblStuas'>";
                tbody += PrizeTaskReocrdStates;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblTaskID' id='" + taskID + "'>";
                tbody += taskName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblRemark'>";
                tbody += txtRemark;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblPrizeCount'>";
                tbody += txtPrizeCount;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb2").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblUserid]").first().text(UserIMSI);
                $(contr).parent().parent().find("lable[name=lblPrizeid]").first().text(PrizeName);
                $(contr).parent().parent().find("lable[name=lblCreatetime]").first().text(CreateTime);
                $(contr).parent().parent().find("lable[name=lblEditTime]").first().text(txtEndTime);
                $(contr).parent().parent().find("lable[name=lblStuas]").first().text(PrizeTaskReocrdStates);
                $(contr).parent().parent().find("lable[name=lblTaskID]").first().text(taskName);
                $(contr).parent().parent().find("lable[name=lblTaskID]").first().attr("id", taskID);
                $(contr).parent().parent().find("lable[name=lblRemark]").first().text(txtRemark);
                $(contr).parent().parent().find("lable[name=lblPrizeCount]").first().text(txtPrizeCount);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Delete(contr) {
    var lbl = $(contr).parent().parent();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/PrizeTaskReocrdAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $(lbl).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
